package cn.chenxing.module.login.controller;

import cn.chenxing.domain.util.DialogBuilderUtil;
import cn.chenxing.domain.util.EnDecodeUtil;
import cn.chenxing.module.login.service.LoginService;
import cn.chenxing.module.user.entity.VenusUserInfoEntity;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Description 注册Controller层
 * @Author maogen.ymg
 * @Date 2020/3/2 12:34
 */
@Lazy
@FXMLController
@Slf4j
public class RegisterController implements Initializable {

    @FXML
    private JFXTextField userName;

    @FXML
    private JFXPasswordField password;

    @FXML
    private JFXPasswordField confirmPassword;

    @FXML
    private JFXTextField mobilePhone;

    @FXML
    private JFXComboBox<String> securityQuestion;

    @FXML
    private JFXTextField securityAnswer;

    @FXML
    private JFXButton register;

    @Autowired
    LoginService loginService;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        List<String> securityQuestionList = loginService.getSecurityQuestion();
        securityQuestion.getItems().addAll(securityQuestionList);
        securityQuestion.setValue(securityQuestionList.get(0));
    }

    @FXML
    private void submit(){
        // 填写内容检查
        checkFilledInfo();

        // 密码MD5，密保Base64
        VenusUserInfoEntity userInfoEntity = new VenusUserInfoEntity();
        userInfoEntity.setUserName(userName.getText().trim())
                .setPassword(EnDecodeUtil.md5(password.getText().trim()))
                .setMobilePhone(mobilePhone.getText().trim())
                // 默认头像
                .setHeadPath("/mnt/venus/headImages/default_head.png")
                .setQuestionIndex(loginService.getQuestionIndex(securityQuestion.getValue()))
                .setAnswer(EnDecodeUtil.toBase64(securityAnswer.getText().trim()));

        // 注册成功
        try {
            if (loginService.register(userInfoEntity)) {
                new DialogBuilderUtil(register).setNegativeBtn("取消", () -> {})
                        .setPositiveBtn("确定",
                                () -> loginService.backLoginView(userName.getScene().getWindow()))
                        .setTitle("注册成功").setMessage("注册成功! 将回到登录界面").create();

            }
        }catch (Exception e){
            DialogBuilderUtil.showPositiveAlert(register,"注册失败", "该用户名已被使用，请重新注册");
        }
    }

    @FXML
    private void back(){
        loginService.backLoginView(userName.getScene().getWindow());
    }

    private void checkFilledInfo(){
        // 完整性检查
        boolean isComplete = StringUtils.isNoneBlank(userName.getText(), password.getText(),
                confirmPassword.getText(), mobilePhone.getText(), securityAnswer.getText());
        DialogBuilderUtil.showPositiveAlert(!isComplete, register,"请检查!", "该请完整填写账号和密码");

        // 格式检查
        DialogBuilderUtil.showPositiveAlert(userName.getText().trim().length() < 2, register,"请注意用户名格式","用户名长度两位以上");
        DialogBuilderUtil.showPositiveAlert(!password.getText().equals(confirmPassword.getText()), register,"请注意密码填写","两次输入的密码不一致");
        DialogBuilderUtil.showPositiveAlert(password.getText().trim().length() < 6, register,"请注意密码长度","密码长度六位以上");
        DialogBuilderUtil.showPositiveAlert(mobilePhone.getText().trim().length() != 11, register,"请注意手机号码格式","手机号码格式不正确");
    }
}
